<template>
  <div>
    <el-dialog
      :title="agentId ? '编辑经纪':'添加经纪'"
      :visible="isShowDialog"
      width="60%"
      center
      @close="CloseShow"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="ruleForm.phone" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="ruleForm.address" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="ruleForm.desc" type="textarea" />
        </el-form-item>
        <el-form-item label="封面">
          <ImageUpload ref="ImgUp" />
        </el-form-item>
      </el-form>
      <!-- 按钮 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button type="primary" size="small" @click="BtnOk">确 定</el-button>
        <el-button size="small" @click="CloseShow">取 消</el-button>
      </el-row>

    </el-dialog>
  </div>
</template>

<script>
// 添加接口
import { Addagents, Oneagents, editagents } from '@/api/economics'

export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      PicShow: false, // 控制图片显示
      ruleForm: {
        name: '',
        email: '',
        phone: '',
        desc: '',
        address: ''
      },
      rules: {
        name: [
          {
            required: true, message: '请输入名称', trigger: 'blur'
          },
          {
            min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'
          }
        ],
        phone: [
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确手机号', trigger: 'blur' },
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],

        email: [
          { message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        desc: [
          {
            required: true, message: '请输入内容', trigger: 'blur'
          }
        ]
      },
      agentId: '' // 经纪人id
    }
  },
  methods: {
    //   关闭前的回调
    CloseShow() {
      // 清空列表数据
      this.ruleForm = {}
      // 清空id
      this.agentId = ''
      //   清空上传组件数据
      this.$refs.ImgUp.fileList = []
      //   清除校验
      this.$refs.ruleForm.resetFields()
      //   关闭弹框
      this.$emit('update:isShowDialog', false)
    },
    // 确认按钮
    async   BtnOk() {
      // 校验表单
      await this.$refs.ruleForm.validate()
      const fileList = this.$refs.ImgUp.fileList // 拿上传组件的上传文件对象
      console.log(86, fileList)

      //   提示用户
      //   如果有id是编辑
      if (this.agentId) {
        // console.log(this.agentId)
        await editagents({
          ...this.ruleForm,
          image: fileList[0] ? fileList[0].url : ''
        })
      } else {
        // 调佣接口
        await Addagents(
          {
            ...this.ruleForm,
            image: fileList[0] ? fileList[0].url : 'http://69hr-1300310660.cos.ap-nanjing.myqcloud.com/122.png'
          }
        )
      }
      await this.$message.success('操作成功')
      //   通知父组件刷新页面
      await this.$emit('Addagent')
      // 清空上传组件数据
      this.$refs.ImgUp.fileList = []
      //   关闭窗口
      this.$emit('update:isShowDialog', false)
    },
    // 父调用子组件的方法进行回显回显
    async  editOneagent(id) {
      this.agentId = id
      const { data } = await Oneagents(id)
      // console.log(120, data)
      this.ruleForm = data

      this.$refs.ImgUp.fileList = [{ url: data.image ? data.image : '' }]
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
